package org.mics.cyy.backUser.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.mics.cache.redis.RedisHelper;
import org.mics.core.model.CurrentUser;
import org.mics.core.page.PageQuery;
import org.mics.core.response.OneDataResponse;
import org.mics.core.response.PageDataResponse;
import org.mics.core.response.SaveOrUpdateResponse;
import org.mics.core.response.SimpleResponse;
import org.mics.cyy.backUser.entity.BackUserDO;
import org.mics.cyy.backUser.request.BackUserRequest;
import org.mics.cyy.backUser.request.EditUserPassRequest;
import org.mics.cyy.backUser.request.LoginRequest;
import org.mics.cyy.backUser.service.BackUserService;
import org.mics.cyy.backUser.vo.BackUserTokenVO;
import org.mics.cyy.backUser.vo.BackUserVO;
import org.mics.lang.bean.BeanConvertUtil;
import org.mics.token.annotation.IgnoreToken;
import org.mics.token.annotation.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 管理端用户
 * @author mics
 * @date 2020年6月10日
 * @version  1.0
 */
@RestController
@RequestMapping("/backUser")
@Tag(name = "[管理端]-用户")
public class BackUserController {
	@Resource
	private BackUserService backUserService;
	@Resource
	private RedisHelper redisHelper;
	private static final Logger LOGGER = LoggerFactory.getLogger(BackUserController.class);
	
    @Operation(summary =  "账号+密码登录")
	@IgnoreToken
    @PostMapping("/login")
	public OneDataResponse<BackUserTokenVO> login(@Validated @RequestBody LoginRequest loginRequest,HttpServletRequest request,HttpServletResponse response) {
		LOGGER.info("账号密码登录[account:{}]", loginRequest.getAccount());
		BackUserTokenVO endUserTokenVO = backUserService.login(loginRequest);
		LOGGER.debug("账号密码登录成功");
		return new OneDataResponse<BackUserTokenVO>(endUserTokenVO);
	}
    
	
    @Operation(summary =  "后台用户列表")
    @RequestMapping(value = "/listByPage",method = RequestMethod.POST)
	public PageDataResponse<BackUserVO> listByPage(@RequestBody PageQuery pageQuery) {
		PageDataResponse<BackUserVO> pageDataResponse =   backUserService.listByPage(pageQuery);
		return pageDataResponse;
	}
    
	@Operation(summary =  "创建用户")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
	public SaveOrUpdateResponse save(@RequestBody  @Valid BackUserRequest backUserRequest) {
		String id =   backUserService.save(backUserRequest);
		return new SaveOrUpdateResponse(id);
	}
	
	@Operation(summary =  "删除用户")
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.DELETE)
	public SimpleResponse save(@PathVariable("id") @Parameter(description = "用户id") String id) {
		backUserService.deleteById(id);
		return new SimpleResponse();
	}
	
	@Operation(summary =  "修改用户密码")
    @RequestMapping(value = "/editPass",method = RequestMethod.POST)
	public SaveOrUpdateResponse editPass(@Valid @RequestBody EditUserPassRequest editUserPassRequest) {
		String id = backUserService.editPass(editUserPassRequest);
		return new SaveOrUpdateResponse(id);
	}
	
	@Operation(summary =  "退出登录")
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
	public SimpleResponse logout(@LoginUser CurrentUser currentUser,HttpServletRequest request,HttpServletResponse response) {
		//清除登录状态
		String token = currentUser.getToken();
		//存储登录信息
        redisHelper.del(token);
		return new SimpleResponse();
	}
	
	
	@Operation(summary =  "获取用户信息")
	@RequestMapping(value = "/info",method = RequestMethod.GET)
    public OneDataResponse<BackUserVO> info(@LoginUser CurrentUser currentUser) {
    	BackUserDO backUserDO = backUserService.info(currentUser.getId());
        LOGGER.debug("根据token获取用户信息成功");
        return new OneDataResponse<BackUserVO>(BeanConvertUtil.convert(backUserDO, BackUserVO.class));
    }
    
}
